using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections.Generic;

public class DomainService : Service
{
    private int domainId;
    private DateTime registerDate;
    private DateTime expiryDate;

    public DateTime RegisterDate
    {
        get { return registerDate; }
        set { registerDate = value; }
    }

    public DateTime ExpiryDate
    {
        get { return expiryDate; }
        set { expiryDate = value; }
    }

    public int DomainId
    {
        get { return domainId; }
        set { domainId = value; }
    }

    /// <summary>
    /// Parameterless constructor for serialization.
    /// </summary>
    public DomainService() { }

    protected DomainService(
        int domainId,
        int serviceId,
        int accountId,
        string title,
        DateTime registerDate,
        DateTime expiryDate,
        DateTime createDate,
        DateTime updateDate)
        : base(serviceId, accountId, title, createDate, updateDate)
    {
        this.domainId = domainId;
        this.registerDate = registerDate;
        this.expiryDate = expiryDate;
    }

    internal static DomainService Create(
        string title, DateTime registerDate, DateTime expiryDate, int accountId)
    {
        int domainId, serviceId;
        using (SqlConnection connection = new SqlConnection(Settings.ConnectionStrings.AccountsDatabase))
        {
            SqlCommand command = new SqlCommand("DomainCreate", connection);
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.AddWithValue("title", title);
            command.Parameters.AddWithValue("registerDate", registerDate);
            command.Parameters.AddWithValue("expiryDate", expiryDate);
            command.Parameters.AddWithValue("accountId", accountId);

            command.Parameters.Add("domainId", SqlDbType.Int).Direction = ParameterDirection.Output;
            command.Parameters.Add("serviceId", SqlDbType.Int).Direction = ParameterDirection.Output;

            connection.Open();
            command.ExecuteNonQuery();
            connection.Close();

            domainId = (int)command.Parameters["domainId"].Value;
            serviceId = (int)command.Parameters["serviceId"].Value;
        }

        return new DomainService(
            domainId, serviceId, accountId, title, registerDate, expiryDate, DateTime.Now, DateTime.Now);
    }

    internal static List<DomainService> Fetch()
    {
        List<DomainService> domainList = new List<DomainService>();
        using (SqlConnection connection = new SqlConnection(Settings.ConnectionStrings.AccountsDatabase))
        {
            SqlCommand command = new SqlCommand("DomainFetch", connection);
            command.CommandType = CommandType.StoredProcedure;

            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                DomainService mailbox = new DomainService(
                    (int)reader["DomainId"],
                    (int)reader["ServiceId"],
                    (int)reader["AccountId"],
                    (string)reader["Title"],
                    (DateTime)reader["RegisterDate"],
                    (DateTime)reader["ExpiryDate"],
                    (DateTime)reader["CreateDate"],
                    (DateTime)reader["UpdateDate"]);

                domainList.Add(mailbox);
            }
            connection.Close();
        }
        return domainList;
    }
}